package com.example.campuslink.ui.activity

import android.os.Handler
import android.os.Looper
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.DrawerLayoutAdapter
import com.example.campuslink.commen.adpter.ViewPagerAdapter
import com.example.campuslink.commen.tools.PermissionManager
import com.example.campuslink.commen.tools.SpacesItemButtonDecoration
import com.example.campuslink.databinding.ActivityMainBinding
import com.example.campuslink.model.DrawerlayoutContentData
import com.example.campuslink.ui.fragment.MainFragment
import com.example.campuslink.ui.fragment.MessageFragment
import com.example.campuslink.ui.fragment.MineFragment
import com.example.campuslink.ui.fragment.ShopFragment
import com.example.campuslink.viewModel.MainViewModel

class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>(
    MainViewModel::class.java,
    R.layout.activity_main,
    BR.viewModel
) {
    companion object {
        private const val AI_NAME = "你好,我是你的AI助理小A!"
    }

    private var index = 0

    private val viewPagerAdapter by lazy {
        ViewPagerAdapter(this)
    }
    private var drawerLayoutAdapter: DrawerLayoutAdapter? = null
    private var handler: Handler? = null

    private val drawList: MutableList<DrawerlayoutContentData> = mutableListOf()

    private val permissionManager = PermissionManager(this)

    override fun initView() {
        initData()
        mViewDataBinding?.init()
        mViewModel?.vpIndexLiveData?.observe(this) {
            mViewDataBinding?.vpFragmentAdapter?.currentItem = it
        }
        mViewDataBinding?.btnAddInvitation?.setOnClickListener {
            permissionManager.requestPublishPermission(this) {
                mViewModel?.jumpToPublicInvitationActivity()
            }
        }
        mViewDataBinding?.rvDrawItem?.adapter = drawerLayoutAdapter
        mViewDataBinding?.rvDrawItem?.layoutManager = LinearLayoutManager(this)
        mViewDataBinding?.rvDrawItem?.addItemDecoration(SpacesItemButtonDecoration(15))
        Glide.with(this).load(R.drawable.ai_img)
            .error(R.drawable.ic_empty)
            .apply(RequestOptions.circleCropTransform())
            .into(mViewDataBinding?.ivAiImage!!)
    }

    fun openDrawerLayout() {
        mViewDataBinding?.mainDrawerlayout?.openDrawer(mViewDataBinding?.layoutDraw!!)
        // 开始显示文字的动画
        startTextAnimation()
    }

    private fun startTextAnimation() {
        handler!!.postDelayed({
            if (index < AI_NAME.length) {
                mViewDataBinding?.tvAiName?.append(AI_NAME.get(index).toString())
                index++
                // 递归调用，继续显示下一个字符
                startTextAnimation()
            }
        }, 100) // 每隔 100 毫秒显示一个字符
    }

    private fun initData() {
        mViewModel?.context = this
        mViewModel?.initDB(this)
        handler = Handler(Looper.getMainLooper())
        drawList.add(DrawerlayoutContentData("咨询问题", R.drawable.ic_call))
        drawList.add(DrawerlayoutContentData("图片生成", R.drawable.ic_music))
        drawList.add(DrawerlayoutContentData("关于我们", R.drawable.about_us))
        drawList.add(DrawerlayoutContentData("退出", R.drawable.close_login))
        drawerLayoutAdapter = DrawerLayoutAdapter(mViewModel!!)
        drawerLayoutAdapter?.setListData(drawList)

    }

    private fun ActivityMainBinding?.init() {
        listOf(
            MainFragment(),
            ShopFragment(),
            MessageFragment(),
            MineFragment()
        ).forEach {
            viewPagerAdapter.addFragment(it)
        }

        mViewDataBinding?.rgRadioGroup?.setOnCheckedChangeListener({ group, checkedId ->
            when (checkedId) {
                R.id.rb_main -> {
                    mViewDataBinding?.vpFragmentAdapter?.currentItem = 0
                }

                R.id.rb_shopping -> {
                    mViewDataBinding?.vpFragmentAdapter?.currentItem = 1
                }

                R.id.rb_message -> {
                    mViewDataBinding?.vpFragmentAdapter?.currentItem = 2
                }

                R.id.rb_mine -> {
                    mViewDataBinding?.vpFragmentAdapter?.currentItem = 3
                }
            }
        })


        mViewDataBinding?.vpFragmentAdapter?.adapter = viewPagerAdapter
        mViewDataBinding?.vpFragmentAdapter?.isUserInputEnabled = false
    }

    override fun onDestroy() {
        super.onDestroy()
        handler?.removeCallbacksAndMessages(null)
    }
}


